package com.asion.vmovie.adapters;

import android.content.Context;
import android.support.v4.util.Pools;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.asion.vmovie.R;
import com.asion.vmovie.beans.LunBoBean;
import com.bumptech.glide.Glide;

import java.util.List;


/**
 * Created by asion on 16-10-19.
 */

public class HeaderViewPagerAdapter extends PagerAdapter {
    private List<LunBoBean.DataBean> list;
    private Context context;
    private Pools.Pool<ImageView> pool = new Pools.SimplePool<>(6);

    public HeaderViewPagerAdapter(Context context,List<LunBoBean.DataBean> list) {
        this.list = list;
        this.context = context;
        LunBoBean.DataBean first = this.list.get(0);
        LunBoBean.DataBean last = this.list.get(list.size() - 1);
        this.list.add(0,last);
        this.list.add(first);
    }

    private OnImageViewClickListener onImageViewClickListener;

    public void setOnImageViewClickListener(OnImageViewClickListener onImageViewClickListener) {
        this.onImageViewClickListener = onImageViewClickListener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        pool.release((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = pool.acquire();
        if (imageView == null) {
            imageView = new ImageView(context);
        }
        Glide.with(context).load(list.get(position).getImage())
                .asBitmap().centerCrop().error(R.mipmap.default_error).into(imageView);
        container.addView(imageView);
        imageView.setOnClickListener(v -> onImageViewClickListener.onImageClick(list.get(position)));
        return imageView;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public interface OnImageViewClickListener{
        void onImageClick(LunBoBean.DataBean dataBean);
    }
}
